package net.toocruel.iqismart.web.controller.common;

import net.toocruel.iqismart.service.exception.BusinessException;
import net.toocruel.iqismart.utils.FileUploadUtils;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/6 上午4:38
 */
@Controller
@RequestMapping("common/upload")
public class CommonUploadController extends AbstractBaseController {

    @Autowired
    private FileUploadUtils fileUploadUtils;

    @PostMapping("")
    @ResponseBody
    public String upload(MultipartFile file,
                         HttpServletRequest request,
                         HttpSession session,
                         RedirectAttributes redirectAttributes
    ) {
        String path = null;
        try {
            path = fileUploadUtils.upload(file.getInputStream(),file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("文件上传失败");
        }

        return  path;

    }
}
